package com.jpress.control;

import java.util.List;

import com.jpress.dao.DAOBlog;
import com.jpress.dao.DAOCustomPage;
import com.jpress.model.CustomPage;

public class CustomPageControl {
	DAOCustomPage daoCustomPage = new DAOCustomPage();
	DAOBlog daoBlog = new DAOBlog();
	
	public boolean cadastrarCustomPage(CustomPage novaCustomPage) {
		try {
			this.daoCustomPage.begin();
			this.daoCustomPage.persist((CustomPage) novaCustomPage);
			this.daoCustomPage.commit();
			return true;
		} catch (Exception e) {
			return false;
		} finally{
			this.daoCustomPage.close();
		}
	}

	public CustomPage getCustomPageById(int id) {
		return this.daoCustomPage.findById(id);
	}

	public boolean excluirCustomPage(CustomPage custompage) {
		try {
			this.daoCustomPage.begin();
			this.daoCustomPage.remove(custompage);
			this.daoCustomPage.commit();
			return true;
		} catch (Exception e) {
			return false;
		} finally{
			this.daoCustomPage.close();
		}
		
		
	}

	public boolean atualizarCustomPage(CustomPage customPageAtual) {
		try {
			this.daoCustomPage.begin();
			this.daoCustomPage.merge(customPageAtual);
			this.daoCustomPage.commit();
			return true;
		} catch (Exception e) {
			return false;
		}finally{
			this.daoCustomPage.close();
		}
	}

	public boolean cadastrarCustomPage(CustomPage customPageNova, int idBlog) {
		try {
			customPageNova.setBlog(daoBlog.findById(idBlog));
			this.daoCustomPage.begin();
			this.daoCustomPage.persist((CustomPage) customPageNova);
			this.daoCustomPage.commit();
			return true;
		} catch (Exception e) {
			return false;
		} finally{
			this.daoCustomPage.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<CustomPage> getCustomPageByBlogId(int id) {
		String query = "SELECT c FROM CustomPage c WHERE c.blog.id = '" + String.valueOf(id) + "' ";
		return this.daoCustomPage.findAllByQuery(query);
	}
}
